﻿package com.reyco1.extension
{
	/*
   	imports
   	*/
	import com.reyco1.extension.StandardExtension;
	import flash.events.Event;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	/*
	class
	*/
	public class FullBrowserExtension extends StandardExtension
	{
		/*
		variables
		*/
		private var tile:BitmapData;
		private var tileContainer:MovieClip;
		private var stageReference:Stage;
		//public var Tile:Class; // uncomment for documention purposes
		/*
		constructor
		*/
		
		/**
		 * Extending this class will allow you to add full browser capability to your projects
		 * To be used by classes which extend flash.display.Sprite to which you wish to apply advanced output functionality to.
		 * 
		 * NOTE:
		 * please keep in mind that in order for this class to work, there must be a bitmab in the library with the "Tile" class name
		 * 
		 * @param $stage : a reference to the site's stage
		 * @return 
		 * 
		 */		
		public function FullBrowserExtension($stage:Stage = null)
		{
			if($stage == null){
				this.stageReference = this.stage
			}else {
				this.stageReference = $stage
			}
			this.initiateStage();
		}
		/*
		functions
		*/
		
		/** @private */
		private function initiateStage():void
		{
			this.tileContainer = new MovieClip();
			this.tile = new Tile(0, 0);
			this.stageReference.addChildAt(this.tileContainer, 0);
					
			this.stageReference.showDefaultContextMenu = false;
			this.stageReference.scaleMode = StageScaleMode.NO_SCALE;
			this.stageReference.align = StageAlign.TOP_LEFT;
			this.stageReference.addEventListener(Event.RESIZE, this.fillBG);
			
			this.resizeBackground();
		}
		
		private function fillBG($event:Event):void
		{
			this.resizeBackground();
		}
		
		private function resizeBackground():void
		{
			this.tileContainer.graphics.clear();
			this.tileContainer.graphics.beginBitmapFill(this.tile);
			this.tileContainer.graphics.drawRect(0, 0, this.stageReference.stageWidth, this.stageReference.stageHeight);
		}
		
		/**
		 * Returns the movieclip to which the tile is being drawn into
		 * @return 
		 * 
		 */		
		public function get background():MovieClip
		{
			return this.tileContainer;
		}
	}
}